package com.iwuyc.fastexcel.ext.data

import cn.idev.excel.metadata.data.ReadCellData
import org.slf4j.LoggerFactory


/**
 * sheet 信息
 * @param sheetNo 工作簿索引，从1开始
 * @param sheetName 工作簿名称
 */
data class SheetInfo(
    val sheetNo: Int, val sheetName: String?, val headMap: MutableMap<Int, ReadCellData<*>> = HashMap()
) {
    override fun toString(): String {
        return """{"sheetIndex": $sheetNo, "sheetName": "$sheetName"}""".trimMargin()
    }
}

/**
 * @param rowIndex 行索引
 * @param cellInfos 每行中列的数据，key为列名，由标题行决定，val为单元格信息
 * 功能说明
 * @author 吴宇春
 * @date 2022/12/28
 * @version 1.0.0
 */
data class ExcelRowInfo(
    val sheetInfo: SheetInfo, val rowIndex: Int, val cellInfos: Map<String, CellInfo>
) {
    private val log = LoggerFactory.getLogger(ExcelRowInfo::class.java)
    override fun toString(): String {
        return """{"rowIndex": $rowIndex, "excelColumnInfos":$cellInfos""".trimMargin()
    }

    fun getByColumnName(columnName: String, defaultVal: String? = null): String? {
        val columnCell = cellInfos[columnName]
        if (columnCell == null || columnCell.cellData.isBlank()) {
            log.warn("columnName:{} not found in row:{}", columnName, rowIndex)
            return defaultVal
        }
        return columnCell.cellData
    }
}

/**
 *
 * @param columnName 列名
 * @param indexInfo 单元格坐标
 * @param cellData 值
 */
data class CellInfo(
    val columnName: String = "", val indexInfo: IndexInfo? = null, val cellData: String
) {
    override fun toString(): String {
        return """{"columnName": "$columnName", "indexInfo": $indexInfo, "columnVal": "$cellData")""".trimMargin()
    }
}

data class IndexInfo(val rowIndex: Int, val columnIndex: Int) {
    override fun toString(): String {
        return """{"rowIndex": $rowIndex, "columnIndex": $columnIndex}""".trimMargin()
    }
}
